package com.zhao.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zhao.pojo.User;
import com.zhao.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserServiceImpl userService;

    //    权限管理
    @RequestMapping("/toTable")
    public String toTable(HttpSession session, Model model) {
        User userinfo = (User) session.getAttribute("userinfo");
        if (!userinfo.getPosition().equals("admin")) {
            model.addAttribute("msg", "你没有权限");
            return "error/404";
        } else return "table";
    }

    //  退出登录
    @RequestMapping("/toLogin")
    public String toLogin(HttpSession session) {
        session.removeAttribute("userinfo");
        return "login";
    }


    //    登录
    @RequestMapping("/toIndex")
    public String toIndex(User user, HttpSession session, Model model) {
        User user2 = userService.selectUserByPWD(user);
        if (user2 != null || session.getAttribute("userinfo") != null) {
            session.setAttribute("userinfo", user2);
            return "index";
        }
        model.addAttribute("msg", "输入有误");
        return "login";
    }

    //    分页查询
    @RequestMapping(value = "/table", produces = "application/text;charset=utf-8")
    @ResponseBody
    public String table(int page, int limit, HttpSession session) {
        session.setAttribute("page", page);
        session.setAttribute("limit", limit);
        List<User> users = userService.selectAllUser(page, limit);
        PageInfo<User> studentPageInfo = new PageInfo<>(users);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", studentPageInfo.getTotal());
        map.put("data", users);
        return JSON.toJSONString(map);
    }

    //    添加用户
    @RequestMapping(value = "/add")
    @ResponseBody
    public String add(@RequestBody User user) {
        userService.addUser(user);
        return "ok";
    }

    /*
     * 带参跳转到更新页面
     * */
    @RequestMapping("/toEdit/{id}")
    public String toEdit(@PathVariable("id") int id, Model model) {
        User user = userService.selectUserById(id);
        model.addAttribute("user", user);
        return "table/edit";
    }

    //    根据id更新
    @RequestMapping("/edit")
    public String edit(@RequestBody User user, HttpSession session) {
        int page = (int) session.getAttribute("page");
        int limit = (int) session.getAttribute("limit");
        userService.updateById(user);
        return "redirect:/table?page=" + page + "&limit=" + limit;
    }

    //    逻辑删除人员
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String delete(@RequestBody User user) {
        userService.deleteUser(user.getId());
        return "ok";
    }
}
